﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dala.Helpers;


namespace Dala.Samples.Image_Resizing
{
    public partial class image_resizing : System.Web.UI.Page
    {
        #region Fields
        String imageSavingPath = @"~/image-upload/";
        String imageDimensionXmlFile = @"~/Image-Resizing/image-dimension.xml";
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Page.Header.DataBind();
        }

        protected void btnUploadImages_Click(object sender, EventArgs e)
        {
            try
            {
                if (fluDefaultImage.HasFile)
                {
                    List<ImageContainer> imageContainerList = new List<ImageContainer>();
                    String fileSavePath = Server.MapPath(imageSavingPath);
                    String imageName = String.Empty;
                    String contentType = String.Empty;
                    String uploadResult = String.Empty;

                    //Default or Main image
                    if (fluDefaultImage.HasFile)
                    {

                        contentType = fluDefaultImage.PostedFile.ContentType.ToLower();
                        byte[] imageBytes = fluDefaultImage.FileBytes;
                        imageName = fluDefaultImage.FileName;

                        ImageContainer imageContainer = new ImageContainer();
                        imageContainer.ContentType = contentType;
                        imageContainer.ImageBytes = imageBytes;
                        imageContainer.ImageName = imageName;
                        imageContainer.ImageResizeType.Add(ImageResizeType.All);

                        imageContainerList.Add(imageContainer);
                    }

                    //Additional Images
                    for (int counter = 1; counter < Request.Files.Count; counter++)
                    {
                        HttpPostedFile fuImageAdditionalAdd = Request.Files[counter];
                        contentType = fuImageAdditionalAdd.ContentType.ToLower();
                        imageName = fuImageAdditionalAdd.FileName;

                        if (fuImageAdditionalAdd.ContentLength > 0 && Request.Files.AllKeys[counter].Equals("fuImageAdditional[]"))
                        {

                            byte[] imageBytes = null;
                            using (var binaryReader = new BinaryReader(fuImageAdditionalAdd.InputStream))
                            {
                                imageBytes = binaryReader.ReadBytes(fuImageAdditionalAdd.ContentLength);
                            }

                            ImageContainer imageContainer = new ImageContainer();
                            imageContainer.ContentType = contentType;
                            imageContainer.ImageBytes = imageBytes;
                            imageContainer.ImageName = imageName;
                            imageContainer.ImageResizeType.Add(ImageResizeType.Detail);
                            imageContainer.ImageResizeType.Add(ImageResizeType.KeepOriginal);

                            imageContainerList.Add(imageContainer);
                        }
                    }

                    uploadResult = ImageHelper.DoImageResize(imageContainerList, fileSavePath, Server.MapPath(imageDimensionXmlFile));

                    if (!String.IsNullOrEmpty(uploadResult))
                    {
                        ucMsgDisplay.DisplayMessage(uploadResult, true);
                        return;
                    }

                    ucMsgDisplay.DisplayMessage(ImageMessage.IMAGE_UPLOAD_SUCCESSFULL, false);
                }
                else
                {
                    ucMsgDisplay.DisplayMessage(ImageMessage.IMAGE_REQUIRED, true);
                }
            }
            catch (Exception ex)
            {
                ucMsgDisplay.DisplayMessage(ex, true);
            }


        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

        }//end methods
    }
}